﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
 
namespace TM.QWeibo.Dal
{

    /// <summary>
    /// 用户资料
    /// </summary>
    public class User:Base
    {
        #region 私有变量
        /// <summary>
        /// 回调
        /// </summary>
        private Action<SDK.Objects.UserInfo> _userCallback { get; set; }

        /// <summary>
        /// API
        /// </summary>
        readonly SDK.API.User _userApi = new SDK.API.User(OAuthInstance);


        #endregion

        /// <summary>
        /// 实例
        /// </summary>
        public static User Instance {
            get {
                return new User();
            }
        }

        /// <summary>
        /// 获取我自己的信息
        /// </summary>
        /// <param name="callback"></param>
        public void MyInfo(Action<SDK.Objects.UserInfo> callback) {
            _userCallback = callback;
            _userApi.GetProfile(GetInfoEnd);
        }
        /// <summary>
        /// 其他用户的信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="callback"></param>
        public void OtherUser(string name, Action<SDK.Objects.UserInfo> callback)
        {
            var rs = Cache.SterlingService.DataBase.Query<SDK.Objects.UserInfo, string>();
            foreach (var tableKey in rs.Where(tableKey => string.Equals(name, tableKey.Key)))
            {
                callback(tableKey.LazyValue.Value);
                return;
            }
            _userCallback = callback;
            _userApi.GetOtherProfile(name,GetInfoEnd);
        }

        /// <summary>
        /// API回调
        /// </summary>
        /// <param name="data"></param>
        private void GetInfoEnd(SDK.Objects.ResponseDataBase<SDK.Objects.UserInfo> data)
        {
            if (_userCallback!=null) {
                _userCallback(data.Data);
                Cache.SterlingService.DataBase.Save(data.Data);
                Cache.SterlingService.DataBase.Flush();
            }
        }
        /// <summary>
        ///搜用户
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageNum"></param>
        /// <param name="callback"></param>
        public void Search(string keyword,int pageNum, Action<ICollection<SDK.Objects.UserInfo>> callback) {

            _userApi.Search(keyword,Common.Params.PageListCount, pageNum, callback);

        }

        /// <summary>
        ///搜用户
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageNum"></param>
        /// <param name="callback"></param>
        public void Search(string keyword, int pageNum, Action<SDK.Objects.ResponseDataObject<SDK.Objects.UserInfo>> callback)
        {

            _userApi.Search(keyword, Common.Params.PageListCount, pageNum, callback);

        }
        /// <summary>
        /// 获取一批人的简单资料
        /// </summary>
        /// <param name="userIds"></param>
        /// <param name="callback"></param>
        public void GetBriefInfos(ICollection<string> userIds, Action<ICollection<SDK.Objects.UserInfo>> callback)
        {
            _userApi.GetBriefInfos(String.Join(",",userIds.ToArray()),callback);
        }

        /// <summary>
        /// 获取一个用户的简单资料
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="callback"></param>
        public void GetBriefInfos(string userId, Action<SDK.Objects.UserInfo> callback)
        {
            GetBriefInfos(new List<string> { userId }, (rs) => callback(rs.FirstOrDefault()));
        }
        /// <summary>
        /// 获取微博列表中用户name和nickname字典
        /// </summary>
        /// <returns></returns>
        public Dictionary<string,string> GetUserDictionary()
        {
            var rs = Cache.SterlingService.DataBase.Query<SDK.Objects.UserInfoBase, string>();
            return rs.ToDictionary(tableKey => tableKey.Key, tableKey => tableKey.LazyValue.Value.NickName);
        }

                /// <summary>
        /// 拉黑
        /// </summary>
        /// <param name="name"></param>
        /// <param name="callback"></param>
        public void AddBlackList(string name,Action<bool> callback)
        {
            _userApi.AddBlackList(name,(b)=>
            {
                if(b)
                {
                    OtherUser(name, (u) =>
                    {
                        try
                        {
                            u._IsMyBlack = 1;
                            Cache.SterlingService.DataBase.Save(u);
                            Cache.SterlingService.DataBase.Flush();
                        }
                        catch
                        {

                        }
                    });
                }
                callback(b);
            });
        }

        /// <summary>
        /// 拉白
        /// </summary>
        /// <param name="name"></param>
        /// <param name="callback"></param>
        public void DeleteBlackList(string name, Action<bool> callback)
        {
            _userApi.DeleteBlackList(name, (b) =>
            {
                if (b)
                {
                    OtherUser(name, (u) =>
                    {
                        try
                        {
                            u._IsMyBlack = 0;
                            Cache.SterlingService.DataBase.Save(u);
                            Cache.SterlingService.DataBase.Flush();
                        }
                        catch
                        {
                                
                        }
                    });
                }
                callback(b);
            });
        }
    }
}
